<template>
    <!-- 对话框表单 -->
    <el-dialog
        class="ba-operate-dialog"
        :close-on-click-modal="false"
        :model-value="baTable.form.operate ? true : false"
        @close="baTable.toggleForm"
    >
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
                {{ baTable.form.operate ? t(baTable.form.operate) : '' }}
            </div>
        </template>
        <el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
            <div
                class="ba-operate-form"
                :class="'ba-' + baTable.form.operate + '-form'"
                :style="'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'"
            >
                <el-form
                    v-if="!baTable.form.loading"
                    ref="formRef"
                    @keyup.enter="baTable.onSubmit(formRef)"
                    :model="baTable.form.items"
                    label-position="right"
                    :label-width="baTable.form.labelWidth + 'px'"
                    :rules="rules"
                    :disabled="baTable.form.operate=='view'"
                    class="flex"
                >
                <!-- <FormItem
                    :label="t('complainthead.CORPCODE')"
                    type="string" v-model="baTable.form.items!.CORPCODE"
                    prop="CORPCODE" :input-attr="{ placeholder: t('Please input field', { field: t('complainthead.CORPCODE') }) }"
                /> -->
                <FormItem :label="t('complainthead.BILLNO')" type="string" v-model="baTable.form.items!.BILLNO" prop="BILLNO" :input-attr="{ placeholder: '保存时自动生成',disabled:true }" />
                <FormItem
                    :label="t('complainthead.CORPCODE')"
                    prop="CORPCODE"
                    v-model="baTable.form.items!.CORPCODE"
                    type="remoteSelect"
                    :input-attr="{
                        pk: 'CORP',
                        field: 'CORP',
                        'remote-url': corplib +'index',
                        placeholder: t('Click Select')
                    }"
                />

                <FormItem :label="t('complainthead.YBH')" type="string" v-model="baTable.form.items!.YBH" prop="YBH" :input-attr="{ placeholder: t('Please input field', { field: t('complainthead.YBH') }) }" />
                <FormItem :label="t('complainthead.BILLDT')" type="datetime" v-model="baTable.form.items!.BILLDT" prop="BILLDT" :input-attr="{ placeholder: t('Please select field', { field: t('complainthead.BILLDT') }) }" />

                <FormItem
                    :label="t('complaintbody.COMPITEM')"
                    v-model="baTable.form.items!.COMPITEM"
                    prop="COMPITEM"
                    type="remoteSelect"
                    :input-attr="{
                        pk: 'code',
                        field: 'code',
                        'remote-url': codelib +'complaint',
                        placeholder: t('Click Select'),
                        onChange:handleTypeChange
                    }"
                />
                <FormItem
                    :label="t('complaintbody.PNAME')"
                    v-model="baTable.form.items!.PNAME"
                    prop="PNAME"
                    type="string"
                    :input-attr="{
                        placeholder: t('Click Select'),
                        disabled:true
                    }"
                />

                <FormItem
                    :label="t('complaintbody.TYPE')"
                    v-model="baTable.form.items!.TYPE"
                    prop="TYPE"
                    type="remoteSelect"
                    :input-attr="{
                        pk: 'code',
                        field: 'codename',
                        'remote-url': codelib +'types',
                        placeholder: t(''),
                    }"
                />
                <FormItem
                    :label="t('complaintbody.TSNAME')"
                    v-model="baTable.form.items!.TSNAME"
                    prop="TSNAME"
                    v-if="baTable.form.items?.TYPE == '2'"
                    type="remoteSelect"
                    :input-attr="{
                        pk: 'CORP',
                        field: 'CORP',
                        'remote-url': corplib +'index?type=2',
                        placeholder: t('')
                    }"
                />
                <FormItem
                    :label="t('complaintbody.TSNAME')"
                    v-model="baTable.form.items!.TSNAME"
                    v-else
                    prop="TSNAME"
                    type="remoteSelect"
                    :input-attr="{
                    pk: 'nickname',
                    field: 'nickname',
                    'remote-url':authAdmin+'index',
                    placeholder: t('Click Select'),
                }"
                />
                <FormItem style="width:100%" :label="t('complaintbody.ZGREMARK')" type="textarea" v-model="baTable.form.items!.ZGREMARK" prop="ZGREMARK" :input-attr="{ rows: 3, placeholder: t('Please input field', { field: t('complaintbody.ZGREMARK') }) }" @keyup.enter.stop="" @keyup.ctrl.enter="baTable.onSubmit(formRef)" />
                <FormItem :label="t('complaintbody.ZGR')" type="string" v-model="baTable.form.items!.ZGR" prop="ZGR" :input-attr="{ placeholder: t('Please input field', { field: t('complaintbody.ZGR') }) }" />
                <FormItem :label="t('complaintbody.ZGDATE')" type="datetime" v-model="baTable.form.items!.ZGDATE" prop="ZGDATE" :input-attr="{ placeholder: t('Please select field', { field: t('complaintbody.ZGDATE') }) }" />
                <FormItem
                    :label="t('complaintbody.ZGFLAG')"
                    v-model="baTable.form.items!.ZGFLAG"
                    type="radio"
                    :data="{ content: { '0': '未完成', '1': '已完成' }, childrenAttr: { border: true } }"
                />
                <!-- <FormItem :label="t('complaintbody.ZGFLAG')" type="string" v-model="baTable.form.items!.ZGFLAG" prop="ZGFLAG" :input-attr="{ placeholder: t('Please input field', { field: t('complaintbody.ZGFLAG') }) }" /> -->
                <FormItem :label="t('complaintbody.ZGMANAGER')" type="string" v-model="baTable.form.items!.ZGMANAGER" prop="ZGMANAGER" :input-attr="{ placeholder: t('Please input field', { field: t('complaintbody.ZGMANAGER') }) }" />
                <!-- 备注 -->
                <FormItem style="width:100%" :label="t('complainthead.REMARK')" type="textarea" v-model="baTable.form.items!.DEMO" prop="REMARK" :input-attr="{ placeholder: t('Please input field', { field: t('complainthead.REMARK') }) }" />
                <FormItem style="width:100%" :label="t('complaintbody.FILENAME')" type="file" v-model="baTable.form.items!.FILENAME" prop="FILENAME" :input-attr="{ placeholder: t('Please input field', { field: t('complaintbody.FILENAME') }) }" />
                <FormItem
                    :label="t('complainthead.TYPIST')"
                    type="string"
                    v-model="baTable.form.items!.TYPIST"
                    prop="TYPIST"
                    :input-attr="{ placeholder: '当前登录用户','disabled':true }"
                />
                <FormItem :label="t('complainthead.TYPISTDT')"
                    type="datetime"
                    v-model="baTable.form.items!.TYPISTDT"
                    prop="TYPISTDT"
                    :input-attr="{ placeholder: '当前日期','disabled':true  }" />
                </el-form>
            </div>
        </el-scrollbar>
        <template #footer v-if="baTable.form.operate!='view'">
            <div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
                <el-button @click="baTable.toggleForm('')">{{ t('Cancel') }}</el-button>
                <el-button v-blur :loading="baTable.form.submitLoading" @click="handleSave" type="primary">
                    {{ baTable.form.operateIds && baTable.form.operateIds.length > 1 ? t('Save and edit next item') : t('Save') }}
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref, inject, onMounted, watchEffect } from 'vue'
import { useI18n } from 'vue-i18n'
import type baTableClass from '/@/utils/baTable'
import FormItem from '/@/components/formItem/index.vue'
import type { ElForm, FormItemRule } from 'element-plus'
import { buildValidatorData } from '/@/utils/validate'
import { corplib,codelib , authAdmin} from '/@/api/controllerUrls'
import { useRoute } from 'vue-router'
import createAxios from '/@/utils/axios'



const formRef = ref<InstanceType<typeof ElForm>>()
const baTable = inject('baTable') as baTableClass

const { t } = useI18n()


watchEffect(()=>{
    // if(baTable.form.operate == 'edit'){
    //     if(baTable.form.items!.COMPITEM){
    //         handleTypeChange(baTable.form.items!.COMPITEM)
    //     }
    // }
})

const rules: Partial<Record<string, FormItemRule[]>> = reactive({
    CORPCODE: [buildValidatorData({ name: 'required', title: t('complainthead.CORPCODE') })],
    BILLDT: [buildValidatorData({name: 'required', message: t('Please enter the correct field', { field: t('complainthead.BILLDT') })})],
    // ZGDATE: [buildValidatorData({name: 'required', message: t('Please enter the correct field', { field: t('complaintbody.ZGDATE') })})],
    YBH: [buildValidatorData({ name: 'required', title: t('complainthead.YBH') })],

    TYPISTDT: [buildValidatorData({name: 'date', message: t('Please enter the correct field', { field: t('complainthead.TYPISTDT') })})],
})

const fnAddZero = (val:Number)=>{
    return val<10?'0'+val:val
}

const handleTypeChange = (val:String)=>{
    createAxios({
        url:`/admin/codelib/complaint?page=1&initKey=code&initValue=${val}&select=true&quick_search=`
    }).then((res:any)=>{
        let item = res.data.list.find((it:any)=>it.code == val)
        baTable.form.items!.PNAME = item.codename
    })
}

const route = useRoute()
const type = route.name === 'complainthead/customer'? 1:2

const handleSave = ()=>{
    baTable.form.items!.type = type
    baTable.onSubmit(formRef.value)
}


</script>

<style scoped lang="scss"></style>
